/*******************************************************************
 *			Methods used for the Textarea-Input-Counter            *
 *                with special Handling of '\n'					   *
 *******************************************************************/
function countChar(htmlElement, maxLength) {
	var text = htmlElement.value;
	
	var newLineCountOverFullText = countNewLinesInText(text);
	var textLength = text.length + newLineCountOverFullText;
	
	if (textLength >= maxLength) {
		if (newLineCountOverFullText == 0) {
			htmlElement.value = text.substring(0, maxLength);
			textLength = maxLength;
		} else {
			var newLineCountOverTextMaxLength = countNewLinesUntilLengthIsOverMaximum(text, maxLength);
			htmlElement.value = text.substring(0, maxLength - newLineCountOverTextMaxLength);
			textLength = htmlElement.value.length + countNewLinesInText(htmlElement.value);
		}
	}
	document.getElementById(htmlElement.id+'Count').value = maxLength - textLength;
};

function countNewLinesInText(text) {
	return text.split('\n').length - 1;
};

function countNewLinesUntilLengthIsOverMaximum(text, maxLength) {
	var currentLength = 0;
	var newLineCount = 0;
	for (var i = 0; i < text.length; i++) {
		var actualChar = text[i];
		if (actualChar == '\n') {
			currentLength += 2;
			newLineCount++;
			if (currentLength >= maxLength) {
				return newLineCount;
			}
		} else {
			currentLength += 1;
			if (currentLength >= maxLength) {
				return newLineCount;
			}
		}
	}
	return newLineCount;
};
/*******************************************************************/


function showMessagePanel(id) {
	$("#" + id).show("slow");
}

function hideMessagePanel(id) {
	$("#" + id).css("display", "none");
}

